import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int [] array = new int[n];
        for (int i = 0; i <n ; i++) {
            int ans=0;
            int ans1=0;
            char [] arr = sc.next().toCharArray();
            //ABABAB
            for (int j = 0; j < arr.length; j++) {
                if(j%2==1&&arr[j]=='B') ans+=1;
                if(j%2==0&&arr[j]=='A') ans+=1;
            }
            for (int j = 0; j < arr.length; j++) {
                if(j%2==1&&arr[j]=='A') ans1+=1;
                if(j%2==0&&arr[j]=='B') ans1+=1;
            }
            array[i]=Math.min(ans,ans1);
        }
        
        for (int i = 0; i < n; i++) {
            System.out.println(array[i]);
        }
    }
}
